/**
 * G6 相关方法
 */
import G6 from '@antv/g6';

// 自定义节点
export const registerNode = (nodeName: string, extendedNodeName: string) => {
    G6.registerNode(
        nodeName,
        {
            afterDraw(cfg, group: any) {
                const box = group.getBBox() // 获取包围盒的坐标和尺寸
                const anchorPoints = (this as any).getAnchorPoints(cfg) // 获取节点的锚点
                // 添加锚点
                anchorPoints.forEach((item: any, index: number) => {
                    group.addShape('circle', {
                        attrs: {
                            r: 5,
                            x: box.x + box.width * item[0],
                            y: box.y + box.height * item[1],
                            fill: '#fff',
                            stroke: '#5F95FF'
                        },
                        name: `node-shape`, // 名称
                        anchorPointIdx: index, // 锚点的索引
                        visible: false,// 是否显示
                        links: 0, // 边数
                        draggable: true, // 允许拖拽
                        cursor: 'pointer',// 鼠标样式
                    })
                });
            },
            getAnchorPoints(cfg: any) {
                return cfg.anchorPoints || getShapePosition(cfg.type)
            },
            // 设置节点的状态
            setState(name, value, item: any) {
                if (name === 'showAnchors') {
                    // 获取所有的锚点
                    const anchorPoints = item.getContainer().findAll((ele: { get: (arg0: string) => string; }) => ele.get('name') === 'node-shape');
                    anchorPoints.forEach((point: any) => {
                        if (value || point.get('links') > 0) point.show()
                        else point.hide()
                    })
                }

            },
        },
        extendedNodeName,
    );
}


// 复制node数据
export const getNodeModel = (combo: any) => {
    // 复制当前node
    const nodes = combo.getNodes().map((node: any) => {
        let n = node.getModel()
        delete n.comboId
        delete n.id
        //  确定node相对于ombo的位置
        n.position = {
            x: combo.getModel().x - n.x,
            y: combo.getModel().y - n.y,
        }
        return n
    })

    return nodes || []
}
// 获取combo
/**
  * @getNodes  获取combo下的node
  * @getCombos 获取combo下的子combo
  */
export const getComboModel = (combo: any, result: any = []) => {
    // 获取子nodes[]
    const no = getNodeModel(combo)
    // 获取子combos[]
    const cs = combo.getCombos()

}

type ShapePosition = [number, number][]
// 获取锚点做标的函数
export const getShapePosition = (type: string): ShapePosition => {
    let anchroPosition: ShapePosition = [[0.5, 0], [1, 0.5], [0.5, 1], [0, 0.5]] // 圆形和方形锚点坐标
    let trianglePosition: ShapePosition = [[0.5, 0], [1, 1], [0, 1]] // 三角形锚点坐标
    let starPosition: ShapePosition = [[0.5, 0], [1, 0.3825], [0.81, 1], [0.19, 1], [0, 0.3825]] // 五角星锚点坐标
    switch (type) {
        case 'rect-node':
            return anchroPosition
        case 'circle-node':
            return anchroPosition
        case 'star-node':
            return starPosition
        case 'triangle-node':
            return trianglePosition
        default:
            return anchroPosition
    }
}
type UpdateShapePosition = {
    x: number,
    y: number
}
// 更新锚点位置的函数
export const updateShapePosition = (model: any, box: any, i: number): UpdateShapePosition => {
    let position: UpdateShapePosition = {
        x: 0,
        y: 0
    }
    if (model.type === "circle-node" || model.type === "rect-node") {
        position = {
            x: (typeFlag(model.size)[0] * getShapePosition(model.type)[i][0]) - (typeFlag(model.size)[0] / 2),
            y: (typeFlag(model.size)[1] * getShapePosition(model.type)[i][1]) - (typeFlag(model.size)[1] / 2)
        }
    }
    if (model.type === 'triangle-node') {
        position = {
            x: box.x + box.width * getShapePosition(model.type)[i][0],
            y: box.x + box.width * getShapePosition(model.type)[i][1]
        }
    }

    if (model.type === 'star-node') {
        position = {
            x: box.width * getShapePosition(model.type)[i][0] - box.width / 2,
            y: box.height * getShapePosition(model.type)[i][1] - box.height / 2 - box.height * 0.05
        }
    }
    return position
}
// 转换size结构
const typeFlag = (size: number | number[]) => {
    if (typeof size === 'number') {
        return [size, size]
    }
    return size
}